package webhook

import (
	awesomeTask "awesomeTask/commom"
	"awesomeTask/commom/timeHelper"
)

const TABLE_NAME = "plugin_web_hook"

/**
添加webhook
*/
func CreateHook(uid int, projectName string, accessToken string, dockerImage string, runShell string) {
	insertParams := map[string]interface{}{
		"uid":          uid,
		"project_name": projectName,
		"access_token": accessToken,
		"docker_image": dockerImage,
		"run_shell":    runShell,
		"create_time":  awesomeTask.Now(),
		"update_time":  awesomeTask.Now(),
	}
	awesomeTask.Insert(TABLE_NAME, insertParams)
}

/**
更新webhook
*/
func UpdateHook(id int, projectName string, accessToken string, dockerImage string, runShell string) {
	insertParams := map[string]interface{}{
		"project_name": projectName,
		"access_token": accessToken,
		"docker_image": dockerImage,
		"run_shell":    runShell,
		"update_time":  awesomeTask.Now(),
	}
	awesomeTask.Update(TABLE_NAME, insertParams, "where id=?", id)
}

/**
删除webhook
*/
func DeleteHook(id int) {
	insertParams := map[string]interface{}{
		"delete_time": timeHelper.NowInt(),
	}
	awesomeTask.Update(TABLE_NAME, insertParams, "where id=?", id)
}
func GetHook(id int) interface{} {
	res := awesomeTask.Query("select * from "+TABLE_NAME+" where id=? and delete_time=0", id)
	if len(res) > 0 {
		return res[0]
	}
	return nil
}
func GetHookByProjectName(projectName string) map[string]interface{} {
	res := awesomeTask.QueryOne("select * from "+TABLE_NAME+" where project_name=? and delete_time=0", projectName)
	return res
}
